from distance import *
from compass import *
from direction import *
from gps import *
import time,android
def travel(route,source,end):
    droid = android.Android()
    end=list(end)
    turn_list=[]
    to_turn_list=[]
#####Finding nearest starting node ahead of the user
    latlon=[(route[0][0],route[0][1]),source]
    nearest=initial((route[1][0],route[1][1]),latlon)
    i=nearest.index(min(nearest))
    route.insert(i,source)
#####Simulating turns and appending to list turn_list and to_turn_list
    az=GetAzimuth()
    src=[route[i][0],route[i][1],0]
    dest=[route[i+1][0],route[i+1][1],0]
    a=(dest[:-1])
    #turn_list.append(a)
    res,trn=turn(az,src,dest)
    droid.ttsSpeak(trn)
    next_az=az+res
    for ll in range(i,len(route)-1):
        src=[route[ll][0],route[ll][1],0]
        dest=[route[ll+1][0],route[ll+1][1],0]
        res,trn=turn(az,src,dest)
        temp=next_az+res
        diff=abs(next_az-temp)
        if diff>0.65:
           a=(route[ll][0],route[ll][1])
           b=(route[ll+1][0],route[ll+1][1])
           turn_list.append(a)
           to_turn_list.append(b)
        next_az=temp
    droid.ttsSpeak("start")
    pos= gps()
    to_turn_list.append(route[-1])
#####Calculation of next turning point and tell direction according to azimuth values
    for i in range(len(turn_list)):
        while (disp(pos,turn_list[i])>0.005):
              r=disp(pos,turn_list[i])
              print r
              #droid.ttsSpeak("proceed")
              pos=gps()
        droid.ttsSpeak("You are approaching a turn")
        pos=gps()
        az=GetAzimuth()
        src=[pos[0],pos[1],0]
        dest=[to_turn_list[i][0],to_turn_list[i][1],0]
        if end!=dest[:-1]:
            res,trn=turn(az,src,dest)
            droid.ttsSpeak(trn)
        else:
            res,trn=turn(az,src,dest)
            droid.ttsSpeak(trn)
            droid.ttsSpeak("you have reached your destination")
